<?php
/*
 * Created on 2010-6-17
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 require('BaseController.php');
 class Admin_UserController extends Admin_BaseController{
 	var $user;
 	function init(){
 		parent::init();
 		$this->user=new User();
 	}

 	/*
 	 *  用户列表
 	 */
 	 function indexAction (){
		 $numPerPage = 10;
		 $getPage = $this->_request->getParam('page');
		 $page = empty($getPage)?'1':$getPage;
		 //抓回所有要显示的记录
		 $array=$this->user->getAllUser();
		//factory出一个分页并且设置当前页码与每页显数
		$paginator = Zend_Paginator::factory($array);
		$paginator->setCurrentPageNumber($page)
				  ->setItemCountPerPage($numPerPage);
         //把分页显示到view
		$this->view->paginator = $paginator;		
		
		echo $this->view->render('user.html');
 	 }
	 /*
	  *添加用户
	  */
	 function addAction (){
		if(isset($_POST['add'])){
			if($this->getRequest()->isPost()){
				$filter=array(
					'*'=>'StringTrim'					
				);
				$validate=array(
					'username'=>array(
						array('StringLength',1,16),
						Zend_Filter_Input::MESSAGES=>array(array(
							Zend_Validate_StringLength::INVALID=>"请输入一个合法的字符串",
							Zend_Validate_StringLength::TOO_SHORT=>"请输入字符长度为1-16",
							Zend_Validate_StringLength::TOO_LONG=>"请输入字符长度为1-16"		
					))
					),
					'password'=>array(
						array('StringLength',1,50),
						Zend_Filter_Input::MESSAGES=>array(array(
							Zend_Validate_StringLength::INVALID=>"请输入一个合法的字符串",
							Zend_Validate_StringLength::TOO_SHORT=>"请输入字符长度为1-50",
							Zend_Validate_StringLength::TOO_LONG=>"请输入字符长度为1-50"	
					))
					),
					'email'=>array('EmailAddress','message'=>array(
						Zend_Validate_EmailAddress::INVALID_FORMAT=>"格式不正确"	
					)),
					'realname'=>array('NotEmpty','message'=>array(
						Zend_Validate_NotEmpty::IS_EMPTY=>"不能为空"	
					)),
					'role'=>array('NotEmpty','message'=>array(
						Zend_Validate_NotEmpty::IS_EMPTY=>"不能为空"
					))
				);
				$input= new Zend_Filter_Input($filter,$validate,$this->getRequest()->getPost());
				if($input->isValid()){
					//print_r($input->getEscaped());
					//验证通过  add user
					if($this->user->unique($input->getEscaped('username'))){
						$this->user->addUser($input->getEscaped());
						$this->view->url=$this->_request->getBaseUrl()."/admin/user/index/"; 
						$this->view->msg="添加用户成功";
						echo $this->view->render('success.html');
					}
					else{
						$this->view->url=$this->_request->getBaseUrl()."/admin/user/add/";
						$this->view->msg="用户名已经存在";
						echo $this->view->render('error.html');
					}
				}
				else{
					$this->view->errors=$input->getMessages();
					$this->view->url=$this->_request->getBaseUrl()."/admin/user/add/";
					$this->view->msg="添加失败";
					echo $this->view->render('error.html');
					//var_dump($input->getMessages());
				}
				//print_r($this->getRequest()->getPost());
			}	
		
		}
		else{
			$this->view->x='add';
			echo $this->view->render('user.html');
		}
		
	 }
	 /*
	  *编辑用户
	  */
	  function editAction(){
		if(isset($_POST['edit'])){
			$id=$this->_request->getPost('uid');
			$data['username']=$this->_request->getPost('username');
			$data['password']=$this->_request->getPost('password');
			$data['email']=$this->_request->getPost('email');
			$data['realname']=$this->_request->getPost('realname');
			$data['role']=$this->_request->getPost('role');
			//var_dump($data);
			$this->user->updateUser($id,$data);
			$this->view->url=$this->_request->getBaseUrl()."/admin/user/";
			$this->view->msg="编辑成功";
			echo $this->view->render('success.html');

		}
		else{
			$id=$this->_request->get('uid');
			if($id){
				$userMsg=$this->user->queryUser($id);
				//print_r($userMsg);
				$this->view->id=$userMsg['uid'];
				$this->view->username=$userMsg['username'];
				$this->view->password=$userMsg['password'];
				$this->view->email=$userMsg['email'];
				$this->view->realname=$userMsg['realname'];
				$this->view->role=$userMsg['role'];
				$this->view->x='edit';
				echo $this->view->render('user.html');
			}
			else{
				$this->view->url=$this->_request->getBaseUrl()."/admin/user/";
				$this->view->msg="要编辑的数据不存在";
				echo $this->view->render('error.html');
			}
		}
	  }
	  /*
	   *删除用户
	   */
	  function deleteAction(){
		$id=(int)$this->_request->get('uid');
		if($id>0){
			$this->user->delUser($id);
			$this->view->url=$this->_request->getBaseUrl()."/admin/user/";
			$this->view->msg="删除成功";
			echo $this->view->render('success.html');
		}
		else{
			$this->view->url=$this->_request->getBaseUrl()."/admin/user/";
			$this->view->msg="要删除的数据不存在";
			echo $this->view->render('error.html');
		}
	  }	

 }
?>
